﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Transactions;
using GenericLibrariesToolKit.Enumeration;

namespace GenericLibrariesToolKit
{
    /// <summary>
    /// 底层DAO接口支撑类
    /// </summary>
    public abstract class DaoSupport<T> : DAO<T>
    {
        /** 实体类型 **/
        protected Type entityClass;

        /// <summary>
        /// 构造函数
        /// </summary>
        public DaoSupport()
        {
           entityClass = GenericUtils.GetSuperClassGenricType(this.GetType());
        }

        public bool Add(T entity)
        {
            return DBUtil.GetInstance().Persist(entity);
        }

        public bool Delete(object entityIdValue)
        {
            return DBUtil.GetInstance().Remove(this.entityClass, entityIdValue);
        }
 
        public long Delete(object[] entityIdValues)
        {
            using (TransactionScope t = new TransactionScope())
            {
                foreach (object entityIdValue in entityIdValues)
                {
                    Delete(entityIdValue);
                }
                t.Complete();
            }

            return entityIdValues.Length;
        }

        public long Delete(WhereCondition[] whereConditions)
        {
            return DBUtil.GetInstance().Remove(this.entityClass, whereConditions);
        }

        public long Delete()
        {
            return DBUtil.GetInstance().Remove(this.entityClass, null);
        }

        public bool Update(T entity)
        {
            return DBUtil.GetInstance().Merge(entity);
        }

        public T Find(object entityIdValue)
        {
            return (T)DBUtil.GetInstance().Find<T>(this.entityClass, entityIdValue);
        }

        public List<T> GetEntityList(string entityId, object entityIdValue,string orderCondition)
        {
            Dictionary<string, string> orderBy =null;

            if (orderCondition != null)
            {
                orderBy  = new Dictionary<string, string>();
                orderBy.Add(orderCondition, "desc");
            }
            
            WhereCondition[] whereCondition = new WhereCondition[]
            {
                new WhereCondition(WhereJoinType.NULL, entityId, MatchType.EQUAL, entityIdValue),
            };

            QueryResult<T> queryResult = GetScrollData(-1, -1, whereCondition, orderBy);

            return queryResult.ResultList;
        }

        public QueryResult<T> GetScrollData(int firstResult, int maxResult, WhereCondition[] whereConditions, Dictionary<string, string> orderByDictionary)
        {
            return (QueryResult<T>)DBUtil.GetInstance().List<T>(this.entityClass, firstResult, maxResult, whereConditions, orderByDictionary);
        }

        public QueryResult<T> GetScrollData(int firstResult, int maxResult, Dictionary<string, string> orderByDictionary)
        {
            return GetScrollData(firstResult, maxResult, null, orderByDictionary);
        }

        public QueryResult<T> GetScrollData(int firstResult, int maxResult, WhereCondition[] whereConditions)
        {
            return GetScrollData(firstResult, maxResult, whereConditions, null);
        }

        public QueryResult<T> GetScrollData(int firstResult, int maxResult)
        {
            return GetScrollData(firstResult, maxResult, null, null);
        }

        public QueryResult<T> GetScrollData()
        {
            return GetScrollData(-1, -1);
        }

        public DataSet GetDataSet(int firstResult, int maxResult, WhereCondition[] whereConditions, Dictionary<string, string> orderByDictionary)
        {
            return DBUtil.GetInstance().GetDataSet(this.entityClass, firstResult, maxResult, whereConditions, orderByDictionary);
        }

        public DataSet GetDataSet(int firstResult, int maxResult, Dictionary<string, string> orderByDictionary)
        {
            return GetDataSet(firstResult, maxResult, null, orderByDictionary);
        }

        public DataSet GetDataSet(int firstResult, int maxResult, WhereCondition[] whereConditions)
        {
            return GetDataSet(firstResult, maxResult, whereConditions, null);
        }

        public DataSet GetDataSet(int firstResult, int maxResult)
        {
            return GetDataSet(firstResult, maxResult, null, null);
        }

        public DataSet GetDataSet()
        {
            return GetDataSet(-1, -1);
        }

    }
}
